package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ProductDetailRequest;
import com.cskaoyan.mall.dto.ProductDetailResponse;
import com.cskaoyan.shopping.converter.ProductCateConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemCat;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.form.PageInfo;
import com.cskaoyan.shopping.service.IItemCatService;
import com.cskaoyan.shopping.service.IProductCateService;
import com.cskaoyan.shopping.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.Id;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author why
 * @since 2022/07/30 15:31
 */
@RestController
@RequestMapping("shopping")
public class ItemCatController {

    @Autowired
    IProductService iProductService;

    @Autowired
    IItemCatService iItemCatService;

    @Autowired
    IProductCateService iProductCateService;


    @GetMapping("/categories")
    public ResponseData categories(AllProductCateRequest request) {

        AllProductCateResponse allProductCateResponse = iItemCatService.selectItemCatAll(request);

        if (ShoppingRetCode.SUCCESS.getCode().equals(allProductCateResponse.getCode())) {
            return new ResponseUtil<>().setData(allProductCateResponse.getProductCateDtoList());
        }

        return new ResponseUtil<>().setErrorMsg(allProductCateResponse.getMsg());
    }

//    @GetMapping("/categories")
//    public ResponseData iCategories(HttpServletRequest httpServletRequest){
//        AllProductCateRequest request = new AllProductCateRequest();
//        AllProductCateResponse response = iProductCateService.getAllProductCate(request);
//        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())){
//            return new ResponseUtil<>().setData(response.getProductCateDtoList());
//        }
//        return new ResponseUtil<>().setErrorMsg(response.getMsg());
//    }


    @GetMapping("/product/{id}")
    public ResponseData productById(@PathVariable("id") long id) {
        ProductDetailRequest request = new ProductDetailRequest();
        request.setId(id);
        ProductDetailResponse productDetailResponse = iItemCatService.selectItemAndDesc(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(productDetailResponse.getCode())) {
            return new ResponseUtil<>().setData(productDetailResponse.getProductDetailDto());
        }
        return new ResponseUtil<>().setErrorMsg(productDetailResponse.getMsg());
    }

    @GetMapping("/goods")
    public ResponseData allGoods(AllProductRequest request) {
        AllProductResponse response = iProductService.getAllProduct(request);
        AllProductResponse_1 allProductResponse = new AllProductResponse_1();
        allProductResponse.setTotal(response.getTotal());
        allProductResponse.setData(response.getProductDtoList());
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil<>().setData(allProductResponse);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @GetMapping("/recommend")
    public ResponseData getRecommendGoods() {

        RecommendResponse response = iProductService.getRecommendGoods();
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())){
            return new ResponseUtil<>().setData(response.getPanelContentItemDtos());
        }
       return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }
}
